VERSIONS = ["2.77", "2.78", "2.79"]
DEFAULT_VERSION = "2.79"

from source.builders import CMakeBuilder
import os, sys
import ext.patch

def processurl(machine, dirs, version):
  URL = "http://bullet.googlecode.com/files/bullet-%(VERSION)s.zip" % {"VERSION" : version}
  if version == "2.79":
    URL = "http://bullet.googlecode.com/files/bullet-%(VERSION)s-rev2440.zip" % {"VERSION" : version}
  return URL

URL = processurl

class BulletBuilder( CMakeBuilder ):
  """ subclass of CMakeBuilder with custom build and install step """

  def targetpath(self):
    return self.directories.enviro + os.sep + "bullet-%(VERSION)s" % {"VERSION" : self.version}

  def patch(self):
    os.chdir( self.extractedpath )
    patchstr = ""
    with open(sys.path[0]+"/library/additions/bullet/DBG_DrawNormals_switch.patch", 'r') as patchfile:
      patchstr = patchfile.read()
    p = ext.patch.fromstring(patchstr)
    print "Patch applied: ", p.apply()
  
  def configure( self ):
    """cmake configure with CMAKE_CONFIGURE options"""

    CMAKE_CONFIGURE = r"-DINSTALL_LIBS=TRUE -DUSE_GLUT=FALSE -DBUILD_DEMOS=FALSE -DCMAKE_DEBUG_POSTFIX='d' -DUSE_DOUBLE_PRECISION=TRUE "
    if not self.machine.isWindows():
      CMAKE_CONFIGURE += " -DBUILD_SHARED_LIBS=TRUE"
    
    CMakeBuilder.configure(self, CMAKE_CONFIGURE)

BUILDER = BulletBuilder

